package com.cs157a.dao;

import java.sql.*;
import java.util.*;
import java.math.*;

/**
 * RdiPart Data Access Object (DAO). !!NOTE: need Connection variable by
 * DBCounnection.getDBConnection() This class contains all database handling
 * that is needed to permanently store and retrieve RdiPart object instances.
 */

public class RdiPartDao {

	private String table = "RDIMMOD";

	// DBConnection need to be created on controller or view side
	// DBConnection dbconn = new DBConnection();

	public void setTable(String tableName) { // set current working table name
		table = tableName;
	}

	public String getTable() { // get current working table name
		return table;
	}

	/**
	 * createValueObject-method. This method is used when the Dao class needs to
	 * create new value object instance. The reason why this method exists is
	 * that sometimes the programmer may want to extend also the valueObject and
	 * then this method can be overrided to return extended valueObject. NOTE:
	 * If you extend the valueObject class, make sure to override the clone()
	 * method in it!
	 */
	public RdiPart createValueObject() {
		return new RdiPart();
	}

	/**
	 * getObject-method. This will create and load valueObject contents from
	 * database using given Primary-Key as identifier. This method is just a
	 * convenience method for the real load-method which accepts the valueObject
	 * as a parameter. Returned valueObject will be created using the
	 * createValueObject() method.
	 */
	public RdiPart getObject(Connection conn, String PartNumber)
			throws NotFoundException, SQLException {

		RdiPart valueObject = createValueObject();
		valueObject.setPartNumber(PartNumber);
		load(conn, valueObject);
		return valueObject;
	}

	/**
	 * load-method. This will load valueObject contents from database using
	 * Primary-Key as identifier. Upper layer should use this so that
	 * valueObject instance is created and only primary-key should be specified.
	 * Then call this method to complete other persistent information. This
	 * method will overwrite all other fields except primary-key and possible
	 * runtime variables. If load can not find matching row, NotFoundException
	 * will be thrown.
	 * 
	 * @param conn
	 *            This method requires working database connection.
	 * @param valueObject
	 *            This parameter contains the class instance to be loaded.
	 *            Primary-key field must be set for this to work properly.
	 */
	public void load(Connection conn, RdiPart valueObject)
			throws NotFoundException, SQLException {

		if (valueObject.getPartNumber() == null) {
			// System.out.println("Can not select without Primary-Key!");
			throw new NotFoundException("Can not select without Primary-Key!");
		}

		String sql = "SELECT * FROM " + table + " WHERE (P_NUMBER = ? ) ";
		PreparedStatement stmt = null;

		try {
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, valueObject.getPartNumber());

			singleQuery(conn, stmt, valueObject);

		} finally {
			if (stmt != null)
				stmt.close();
		}
	}

	/**
	 * LoadAll-method. This will read all contents from database table and build
	 * a List containing valueObjects. Please note, that this method will
	 * consume huge amounts of resources if table has lot's of rows. This should
	 * only be used when target tables have only small amounts of data.
	 * 
	 * @param conn
	 *            This method requires working database connection.
	 */
	public List loadAll(Connection conn) throws SQLException {

		String sql = "SELECT * FROM " + table + " ORDER BY P_NUMBER ASC ";
		List searchResults = listQuery(conn, conn.prepareStatement(sql));

		return searchResults;
	}

	/**
	 * create-method. This will create new row in database according to supplied
	 * valueObject contents. Make sure that values for all NOT NULL columns are
	 * correctly specified. Also, if this table does not use automatic
	 * surrogate-keys the primary-key must be specified. After INSERT command
	 * this method will read the generated primary-key back to valueObject if
	 * automatic surrogate-keys were used.
	 * 
	 * @param conn
	 *            This method requires working database connection.
	 * @param valueObject
	 *            This parameter contains the class instance to be created. If
	 *            automatic surrogate-keys are not used the Primary-key field
	 *            must be set for this to work properly.
	 */
	public synchronized void create(Connection conn, RdiPart valueObject)
			throws SQLException {

		String sql = "";
		PreparedStatement stmt = null;
		ResultSet result = null;

		try {
			sql = "INSERT INTO " + table + " ( P_NUMBER, CORE, INHEAD, "
					+ "OUTHEAD, INCON, OUCON, " + "TMOUNT, OILCOOL, PRICE, "
					+ "AMOUNT) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";
			stmt = conn.prepareStatement(sql);

			stmt.setString(1, valueObject.getPartNumber());
			stmt.setString(2, valueObject.getCore());
			stmt.setString(3, valueObject.getInHead());
			stmt.setString(4, valueObject.getOutHead());
			stmt.setString(5, valueObject.getInCon());
			stmt.setString(6, valueObject.getOuCon());
			stmt.setString(7, valueObject.getTMount());
			stmt.setString(8, valueObject.getOilCool());
			stmt.setDouble(9, valueObject.getPrice());
			stmt.setInt(10, valueObject.getAmount());

			int rowcount = databaseUpdate(conn, stmt);
			if (rowcount != 1) {
				// System.out.println("PrimaryKey Error when updating DB!");
				throw new SQLException("PrimaryKey Error when updating DB!");
			}

		} finally {
			if (stmt != null)
				stmt.close();
		}

	}

	/**
	 * Here we are creating temp table
	 * 
	 * @param conn
	 *            This method requires working database connection.
	 * @param tableName
	 *            This is used for table name reference.
	 */
	public void createTmpTable(Connection conn, String tableName)
			throws NotFoundException, SQLException {

		String sql = null;
		if (tableName.equals("RDIMARS") || tableName.equals("RDIMBEH")
				|| tableName.equals("RDIMDAN") || tableName.equals("RDIMMOD"))
			sql = "CREATE TABLE TEMP(P_NUMBER	VARCHAR2(6)	NOT NULL,CORE		VARCHAR2(32),INHEAD		VARCHAR2(17),OUTHEAD	VARCHAR2(17),INCON		VARCHAR2(14),OUCON		VARCHAR2(14),TMOUNT		VARCHAR2(24),OILCOOL	VARCHAR2(10), PRICE		NUMBER(8, 2),AMOUNT		NUMBER(10),PRIMARY KEY	(P_NUMBER))";

		else if (tableName.equals("RADCRX"))
			sql = "CREATE TABLE TEMP(RLINK		NUMBER(4), ARS1		VARCHAR2(3), ARS2		VARCHAR2(3), ARS3		VARCHAR2(3),ARS4		VARCHAR2(3), MOD1		VARCHAR2(3), MOD2		VARCHAR2(3),  MOD3		VARCHAR2(3), MOD4		VARCHAR2(3), BEH1		VARCHAR2(3),BEH2		VARCHAR2(3),BEH3		VARCHAR2(3) BEH4		VARCHAR2(3),DAN1		VARCHAR2(3), DAN2		VARCHAR2(3),DAN3		VARCHAR2(3), DAN4		VARCHAR2(3),PRIMARY KEY	(RLINK));";
		else
			sql = "CREATE TABLE TEMP(MODEL VARCHAR2(25)    NOT NULL, YEAR       VARCHAR2(4) NOT NULL, DESCRIPTION    VARCHAR2(51),LITRES     VARCHAR2(4), ENGINE_TYPE    VARCHAR2(3), CUBIC_INCHES   VARCHAR2(6), RLINK      NUMBER(4), PRIMARY KEY    (MODEL, YEAR, DESCRIPTION, LITRES, ENGINE_TYPE, CUBIC_INCHES), FOREIGN KEY    (RLINK) REFERENCES RADCRX)";

		PreparedStatement stmt = null;

		try {
			stmt = conn.prepareStatement(sql);
			stmt.executeQuery();
		} finally {
			if (stmt != null)
				stmt.close();
		}
	}

	/**
	 * Here we are inserting data in to temp table.
	 * 
	 * @param conn
	 *            This method requires working database connection.
	 * @param tableName
	 *            This is used for table name reference.
	 */
	public void insertTmpTable(Connection conn, String tableName)
			throws NotFoundException, SQLException {

		String sql = "insert into temp select * from " + tableName;

		PreparedStatement stmt = null;

		try {
			stmt = conn.prepareStatement(sql);
			stmt.executeQuery();
		} finally {
			if (stmt != null)
				stmt.close();
		}
	}

	/**
	 * Here we are dropping the temp table
	 * 
	 * @param conn
	 *            This method requires working database connection.
	 */
	public void dropTmpTable(Connection conn) throws NotFoundException,
			SQLException {

		String sql = "drop table temp";

		PreparedStatement stmt = null;

		try {
			stmt = conn.prepareStatement(sql);
			stmt.executeQuery();
		} finally {
			if (stmt != null)
				stmt.close();
		}
	}

	/**
	 * save-method. This method will save the current state of valueObject to
	 * database. Save can not be used to create new instances in database, so
	 * upper layer must make sure that the primary-key is correctly specified.
	 * Primary-key will indicate which instance is going to be updated in
	 * database. If save can not find matching row, NotFoundException will be
	 * thrown.
	 * 
	 * @param conn
	 *            This method requires working database connection.
	 * @param valueObject
	 *            This parameter contains the class instance to be saved.
	 *            Primary-key field must be set for this to work properly.
	 */
	public void save(Connection conn, RdiPart valueObject,
			RdiPart newValueObject) throws NotFoundException, SQLException {

		String sql = "";
		PreparedStatement stmt = null;
		System.out.println("OLD PART = " + valueObject.getPartNumber());
		System.out.println("NEW PART = " + newValueObject.getPartNumber());
		try {
			if (valueObject.getPartNumber().equals(
					newValueObject.getPartNumber())) {
				sql = "UPDATE "
						+ table
						+ " SET CORE = ?, INHEAD = ?, OUTHEAD = ?, "
						+ "INCON = ?, OUCON = ?, TMOUNT = ?, "
						+ "OILCOOL = ?, PRICE = ?, AMOUNT = ? WHERE (P_NUMBER = ? ) ";
				stmt = conn.prepareStatement(sql);
				stmt.setString(1, newValueObject.getCore());
				stmt.setString(2, newValueObject.getInHead());
				stmt.setString(3, newValueObject.getOutHead());
				stmt.setString(4, newValueObject.getInCon());
				stmt.setString(5, newValueObject.getOuCon());
				stmt.setString(6, newValueObject.getTMount());
				stmt.setString(7, newValueObject.getOilCool());
				stmt.setDouble(8, newValueObject.getPrice());
				stmt.setInt(9, newValueObject.getAmount());
				stmt.setString(10, newValueObject.getPartNumber());
				int rowcount = databaseUpdate(conn, stmt);
				System.out.println(stmt + newValueObject.toString());
				System.out.println(rowcount);
				if (rowcount == 0) {
					// System.out.println("Object could not be saved! (PrimaryKey not found)");
					throw new NotFoundException(
							"Object could not be saved! (PrimaryKey not found)");
				}
				if (rowcount > 1) {
					// System.out.println("PrimaryKey Error when updating DB! (Many objects were affected!)");
					throw new SQLException(
							"PrimaryKey Error when updating DB! (Many objects were affected!)");
				}
			} else {
				conn.setAutoCommit(false);// begin transaction
				Savepoint savepoint = conn.setSavepoint("partSavePoint");
				try {
					sql = "INSERT INTO "
							+ table
							+ " ( P_NUMBER, CORE, INHEAD, "
							+ "OUTHEAD, INCON, OUCON, TMOUNT, OILCOOL, PRICE, AMOUNT) VALUES ("
							+ "'" + newValueObject.getPartNumber() + "', '"
							+ newValueObject.getCore() + "', '"
							+ newValueObject.getInHead() + "', '"
							+ newValueObject.getOutHead() + "', '"
							+ newValueObject.getInCon() + "', '"
							+ newValueObject.getOuCon() + "', '"
							+ newValueObject.getTMount() + "', '"
							+ newValueObject.getOilCool() + "', "
							+ newValueObject.getPrice() + ", "
							+ newValueObject.getAmount() + ")";
					Statement stmts = conn.createStatement();
					stmts.addBatch(sql);

					String newpart = newValueObject.getPartNumber();
					String opart = valueObject.getPartNumber();
					stmts.addBatch("UPDATE RADCRX SET ARS1 = '" + newpart
							+ "' WHERE ARS1 = '" + opart + "'");
					stmts.addBatch("UPDATE RADCRX SET ARS2 = '" + newpart
							+ "' WHERE ARS2 = '" + opart + "'");
					stmts.addBatch("UPDATE RADCRX SET ARS3 = '" + newpart
							+ "' WHERE ARS3 = '" + opart + "'");
					stmts.addBatch("UPDATE RADCRX SET ARS4 = '" + newpart
							+ "' WHERE ARS4 = '" + opart + "'");
					stmts.addBatch("UPDATE RADCRX SET BEH1 = '" + newpart
							+ "' WHERE BEH1 = '" + opart + "'");
					stmts.addBatch("UPDATE RADCRX SET BEH2 = '" + newpart
							+ "' WHERE BEH2 = '" + opart + "'");
					stmts.addBatch("UPDATE RADCRX SET BEH3 = '" + newpart
							+ "' WHERE BEH3 = '" + opart + "'");
					stmts.addBatch("UPDATE RADCRX SET BEH4 = '" + newpart
							+ "' WHERE BEH4 = '" + opart + "'");
					stmts.addBatch("UPDATE RADCRX SET DAN1 = '" + newpart
							+ "' WHERE DAN1 = '" + opart + "'");
					stmts.addBatch("UPDATE RADCRX SET DAN2 = '" + newpart
							+ "' WHERE DAN2 = '" + opart + "'");
					stmts.addBatch("UPDATE RADCRX SET DAN3 = '" + newpart
							+ "' WHERE DAN3 = '" + opart + "'");
					stmts.addBatch("UPDATE RADCRX SET DAN4 = '" + newpart
							+ "' WHERE DAN4 = '" + opart + "'");
					stmts.addBatch("UPDATE RADCRX SET MOD1 = '" + newpart
							+ "' WHERE MOD1 = '" + opart + "'");
					stmts.addBatch("UPDATE RADCRX SET MOD2 = '" + newpart
							+ "' WHERE MOD2 = '" + opart + "'");
					stmts.addBatch("UPDATE RADCRX SET MOD3 = '" + newpart
							+ "' WHERE MOD3 = '" + opart + "'");
					stmts.addBatch("UPDATE RADCRX SET MOD4 = '" + newpart
							+ "' WHERE MOD4 = '" + opart + "'");
					stmts.addBatch("DELETE " + table + " WHERE P_NUMBER = '"
							+ opart + "'");

					int result[] = stmts.executeBatch();
					conn.commit();// end transaction
				} catch (Exception exc) {
					conn.rollback(savepoint);// cancel transaction, rollback to
												// savepoint
					exc.printStackTrace();
				}
				conn.setAutoCommit(true);// exit transaction
			}

		} finally {
			if (stmt != null)
				stmt.close();
		}
	}

	/**
	 * delete-method. This method will remove the information from database as
	 * identified by by primary-key in supplied valueObject. Once valueObject
	 * has been deleted it can not be restored by calling save. Restoring can
	 * only be done using create method but if database is using automatic
	 * surrogate-keys, the resulting object will have different primary-key than
	 * what it was in the deleted object. If delete can not find matching row,
	 * NotFoundException will be thrown.
	 * 
	 * @param conn
	 *            This method requires working database connection.
	 * @param valueObject
	 *            This parameter contains the class instance to be deleted.
	 *            Primary-key field must be set for this to work properly.
	 */
	public void delete(Connection conn, RdiPart valueObject)
			throws NotFoundException, SQLException {

		if (valueObject.getPartNumber() == null) {
			// System.out.println("Can not delete without Primary-Key!");
			throw new NotFoundException("Can not delete without Primary-Key!");
		}

		String sql = "DELETE FROM " + table + " WHERE (P_NUMBER = ? ) ";
		PreparedStatement stmt = null;

		try {
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, valueObject.getPartNumber());

			int rowcount = databaseUpdate(conn, stmt);
			if (rowcount == 0) {
				// System.out.println("Object could not be deleted (PrimaryKey not found)");
				throw new NotFoundException(
						"Object could not be deleted! (PrimaryKey not found)");
			}
			if (rowcount > 1) {
				// System.out.println("PrimaryKey Error when updating DB! (Many objects were deleted!)");
				throw new SQLException(
						"PrimaryKey Error when updating DB! (Many objects were deleted!)");
			}
		} finally {
			if (stmt != null)
				stmt.close();
		}
	}

	/**
	 * deleteAll-method. This method will remove all information from the table
	 * that matches this Dao and ValueObject couple. This should be the most
	 * efficient way to clear table. Once deleteAll has been called, no
	 * valueObject that has been created before can be restored by calling save.
	 * Restoring can only be done using create method but if database is using
	 * automatic surrogate-keys, the resulting object will have different
	 * primary-key than what it was in the deleted object. (Note, the
	 * implementation of this method should be different with different DB
	 * backends.)
	 * 
	 * @param conn
	 *            This method requires working database connection.
	 */
	public void deleteAll(Connection conn) throws SQLException {

		String sql = "DELETE FROM " + table + "";
		PreparedStatement stmt = null;

		try {
			stmt = conn.prepareStatement(sql);
			int rowcount = databaseUpdate(conn, stmt);
		} finally {
			if (stmt != null)
				stmt.close();
		}
	}

	/**
	 * coutAll-method. This method will return the number of all rows from table
	 * that matches this Dao. The implementation will simply execute
	 * "select count(primarykey) from table". If table is empty, the return
	 * value is 0. This method should be used before calling loadAll, to make
	 * sure table has not too many rows.
	 * 
	 * @param conn
	 *            This method requires working database connection.
	 */
	public int countAll(Connection conn) throws SQLException {

		String sql = "SELECT count(*) FROM " + table + "";
		PreparedStatement stmt = null;
		ResultSet result = null;
		int allRows = 0;

		try {
			stmt = conn.prepareStatement(sql);
			result = stmt.executeQuery();

			if (result.next())
				allRows = result.getInt(1);
		} finally {
			if (result != null)
				result.close();
			if (stmt != null)
				stmt.close();
		}
		return allRows;
	}

	/**
	 * searchMatching-Method. This method provides searching capability to get
	 * matching valueObjects from database. It works by searching all objects
	 * that match permanent instance variables of given object. Upper layer
	 * should use this by setting some parameters in valueObject and then call
	 * searchMatching. The result will be 0-N objects in a List, all matching
	 * those criteria you specified. Those instance-variables that have NULL
	 * values are excluded in search-criteria.
	 * 
	 * @param conn
	 *            This method requires working database connection.
	 * @param valueObject
	 *            This parameter contains the class instance where search will
	 *            be based. Primary-key field should not be set.
	 */
	public List searchMatching(Connection conn, RdiPart valueObject)
			throws SQLException {

		List searchResults;

		boolean first = true;
		StringBuffer sql = new StringBuffer("SELECT * FROM " + table
				+ " WHERE 1=1 ");

		if (valueObject.getPartNumber() != null) {
			if (first) {
				first = false;
			}
			sql.append("AND P_NUMBER LIKE '")
					.append(valueObject.getPartNumber()).append("%' ");
		}

		if (valueObject.getCore() != null) {
			if (first) {
				first = false;
			}
			sql.append("AND CORE LIKE '").append(valueObject.getCore())
					.append("%' ");
		}

		if (valueObject.getInHead() != null) {
			if (first) {
				first = false;
			}
			sql.append("AND INHEAD LIKE '").append(valueObject.getInHead())
					.append("%' ");
		}

		if (valueObject.getOutHead() != null) {
			if (first) {
				first = false;
			}
			sql.append("AND OUTHEAD LIKE '").append(valueObject.getOutHead())
					.append("%' ");
		}

		if (valueObject.getInCon() != null) {
			if (first) {
				first = false;
			}
			sql.append("AND INCON LIKE '").append(valueObject.getInCon())
					.append("%' ");
		}

		if (valueObject.getOuCon() != null) {
			if (first) {
				first = false;
			}
			sql.append("AND OUCON LIKE '").append(valueObject.getOuCon())
					.append("%' ");
		}

		if (valueObject.getTMount() != null) {
			if (first) {
				first = false;
			}
			sql.append("AND TMOUNT LIKE '").append(valueObject.getTMount())
					.append("%' ");
		}

		if (valueObject.getOilCool() != null) {
			if (first) {
				first = false;
			}
			sql.append("AND OILCOOL LIKE '").append(valueObject.getOilCool())
					.append("%' ");
		}

		if (valueObject.getPrice() != 0) {
			if (first) {
				first = false;
			}
			sql.append("AND PRICE = ").append(valueObject.getPrice())
					.append(" ");
		}

		if (valueObject.getAmount() != 0) {
			if (first) {
				first = false;
			}
			sql.append("AND AMOUNT = ").append(valueObject.getAmount())
					.append(" ");
		}

		sql.append("ORDER BY P_NUMBER ASC ");

		// Prevent accidential full table results.
		// Use loadAll if all rows must be returned.
		if (first)
			searchResults = new ArrayList();
		else
			searchResults = listQuery(conn,
					conn.prepareStatement(sql.toString()));

		return searchResults;
	}

	/**
	 * databaseUpdate-method. This method is a helper method for internal use.
	 * It will execute all database handling that will change the information in
	 * tables. SELECT queries will not be executed here however. The return
	 * value indicates how many rows were affected. This method will also make
	 * sure that if cache is used, it will reset when data changes. Transaction
	 * Added
	 * 
	 * @param conn
	 *            This method requires working database connection.
	 * @param stmt
	 *            This parameter contains the SQL statement to be excuted.
	 */
	protected int databaseUpdate(Connection conn, PreparedStatement stmt)
			throws SQLException {

		int result = -1;
		conn.setAutoCommit(false);// begin transaction
		Savepoint savepoint = conn.setSavepoint("partSavePoint");
		try {
			result = stmt.executeUpdate();
			conn.commit();// end transaction
		} catch (Exception exc) {
			conn.rollback(savepoint);// cancel transaction, rollback to
										// savepoint
			exc.printStackTrace();
		}
		conn.setAutoCommit(true);// exit transaction
		return result;
	}

	/**
	 * databaseQuery-method. This method is a helper method for internal use. It
	 * will execute all database queries that will return only one row. The
	 * resultset will be converted to valueObject. If no rows were found,
	 * NotFoundException will be thrown.
	 * 
	 * @param conn
	 *            This method requires working database connection.
	 * @param stmt
	 *            This parameter contains the SQL statement to be excuted.
	 * @param valueObject
	 *            Class-instance where resulting data will be stored.
	 */
	protected void singleQuery(Connection conn, PreparedStatement stmt,
			RdiPart valueObject) throws NotFoundException, SQLException {

		ResultSet result = null;

		try {
			result = stmt.executeQuery();

			if (result.next()) {

				valueObject.setVendor(table);
				valueObject.setPartNumber(result.getString("P_NUMBER"));
				valueObject.setCore(result.getString("CORE"));
				valueObject.setInHead(result.getString("INHEAD"));
				valueObject.setOutHead(result.getString("OUTHEAD"));
				valueObject.setInCon(result.getString("INCON"));
				valueObject.setOuCon(result.getString("OUCON"));
				valueObject.setTMount(result.getString("TMOUNT"));
				valueObject.setOilCool(result.getString("OILCOOL"));
				valueObject.setPrice(result.getDouble("PRICE"));
				valueObject.setAmount(result.getInt("AMOUNT"));

			} else {
				// System.out.println(table+" Object Not Found!");
				throw new NotFoundException(table + " Object Not Found!");
			}
		} finally {
			if (result != null)
				result.close();
			if (stmt != null)
				stmt.close();
		}
	}

	/**
	 * databaseQuery-method. This method is a helper method for internal use. It
	 * will execute all database queries that will return multiple rows. The
	 * resultset will be converted to the List of valueObjects. If no rows were
	 * found, an empty List will be returned.
	 * 
	 * @param conn
	 *            This method requires working database connection.
	 * @param stmt
	 *            This parameter contains the SQL statement to be excuted.
	 */
	protected List listQuery(Connection conn, PreparedStatement stmt)
			throws SQLException {

		ArrayList searchResults = new ArrayList();
		ResultSet result = null;

		try {
			result = stmt.executeQuery();

			while (result.next()) {
				RdiPart temp = createValueObject();

				temp.setVendor(table);
				temp.setPartNumber(result.getString("P_NUMBER"));
				temp.setCore(result.getString("CORE"));
				temp.setInHead(result.getString("INHEAD"));
				temp.setOutHead(result.getString("OUTHEAD"));
				temp.setInCon(result.getString("INCON"));
				temp.setOuCon(result.getString("OUCON"));
				temp.setTMount(result.getString("TMOUNT"));
				temp.setOilCool(result.getString("OILCOOL"));
				temp.setPrice(result.getDouble("PRICE"));
				temp.setAmount(result.getInt("AMOUNT"));

				searchResults.add(temp);
			}

		} finally {
			if (result != null)
				result.close();
			if (stmt != null)
				stmt.close();
		}

		return (List) searchResults;
	}

	/**
	 * main routine for testing purpose
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DBConnection dbconn = new DBConnection();

		RdiPartDao partdao = new RdiPartDao();
		RdiPart apart = new RdiPart();
		System.out.println("current table=" + partdao.getTable());
		try {
			apart = partdao.getObject(dbconn.getDBConnection(), "883");
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(apart.toString());

		dbconn.disconnectFromDB();

	}

}
